package com.vmc.strategy;

import com.vmc.exception.VMCException;

public abstract class AbstractAction<T> implements
		Action<T> {
	
	private Action<T> chainTo;
	private Action<T> parentAction;

	@Override
	public Action<T> chainedTo(Action<T> chainTo) {
		this.chainTo = chainTo;
		return this;
	}

	@Override
	public void setParent(Action<T> parentAction) {
		this.parentAction = parentAction;
	}

	@Override
	public Action<T> getParent() {
		return this.parentAction;
	}

	@Override
	public Action<T> getChainedAction() {
		return this.chainTo;
	}
	
	protected boolean performNextAction(T actionableItem) throws VMCException {
		if (actionableItem != null && this.getChainedAction() != null) {
			return this.getChainedAction().performAction(actionableItem);
		}
		return true;
	}
	
	@Override
	public boolean performAction(T actionableItem) throws VMCException {
		return this.performMainAction(actionableItem) && this.performNextAction(actionableItem);
	}
	
	abstract boolean performMainAction(T actionableItem) throws VMCException;
}
